//
//  BConversionInputView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2019/1/4.
//  Copyright © 2019 guoshikeji. All rights reserved.
//

#import "BConversionInputView.h"

static NSString *BConversionInputViewCharString = @"123456789.0<";


@interface BConversionInputView ()

@property (nonatomic, strong) UIView *buttonContentView;

/**
 *separaterLayer
 */
@property (nonatomic, strong) CAShapeLayer *separaterLayer;

@end

@implementation BConversionInputView


- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor qmui_colorWithHexString:@"#F6F5FA"];
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    //设置按钮
    [self setupButtons];
    //
    self.separaterLayer = [CAShapeLayer layer];
    self.separaterLayer.lineWidth = 1.0;
    self.separaterLayer.strokeColor = [UIColor qmui_colorWithHexString:@"#CCCCCC"].CGColor;
    [self.layer addSublayer:self.separaterLayer];
}
- (void)setupButtons {
    self.buttonContentView = [UIView new];
    [self addSubview:self.buttonContentView];
    [self.buttonContentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self).offset(0);
    }];
    
    NSString *charString = BConversionInputViewCharString;
    NSInteger buttonCount = charString.length;
    /**
     高度为content的1/4
     宽度为1/3
     */
    QMUIButton *prefrenceAbove;
    QMUIButton *preferenceLeft;
    for (int i=0; i<buttonCount; i++) {
        QMUIButton *button = [QMUIButton new];
        [button setTitleColor:[UIColor qmui_colorWithHexString:@"#000000"] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:23 weight:UIFontWeightRegular];
        [self.buttonContentView addSubview:button];
        [button addTarget:self action:@selector(buttonDidClickedAction:) forControlEvents:UIControlEventTouchUpInside];
        //
        NSString *title = [charString substringWithRange:NSMakeRange(i, 1)];
        [button setTitle:title forState:UIControlStateNormal];
        //
        //
        if (i%3==0) {
            preferenceLeft = nil;
        }
        //
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(self.buttonContentView).multipliedBy(1.0/3);
            make.height.mas_equalTo(self.buttonContentView).multipliedBy(1.0/4);
            if (preferenceLeft) {
                make.left.mas_equalTo(preferenceLeft.mas_right);
            }else {
                make.left.mas_equalTo(self.buttonContentView);
            }
            if (prefrenceAbove) {
                if (preferenceLeft) {
                    make.top.mas_equalTo(prefrenceAbove.mas_top);
                }else {
                    make.top.mas_equalTo(prefrenceAbove.mas_bottom);
                }
            }else {
                make.top.mas_equalTo(self.buttonContentView);
            }
        }];
        preferenceLeft = button;
        prefrenceAbove = button;
    }
}


- (void)setNeedSafeBottomArea:(BOOL)needSafeBottomArea {
    _needSafeBottomArea = needSafeBottomArea;
    CGFloat offset = needSafeBottomArea?-Height_Bottom_SafeArea:0;
    [self.buttonContentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self).offset(offset);
    }];
    [self layoutIfNeeded];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    //
    self.separaterLayer.frame = self.bounds;
    //
    [self drawSeparaterLayerPath];
}
//
- (void)drawSeparaterLayerPath {
    CGFloat width = CGRectGetWidth(self.frame);
    CGFloat height = CGRectGetHeight(self.frame);
    //
    UIBezierPath *bPath = [UIBezierPath bezierPath];
    //两根竖线
    CGFloat xPadding = width/3;
    [bPath moveToPoint:CGPointMake(xPadding, 0)];
    [bPath addLineToPoint:CGPointMake(xPadding, height)];
    [bPath moveToPoint:CGPointMake(xPadding*2, 0)];
    [bPath addLineToPoint:CGPointMake(xPadding*2, height)];
    //三根横线
    CGFloat yPadding = (height-(self.needSafeBottomArea?Height_Bottom_SafeArea:0))/4.0;
    [bPath moveToPoint:CGPointMake(0, yPadding)];
    [bPath addLineToPoint:CGPointMake(width, yPadding)];
    [bPath moveToPoint:CGPointMake(0, yPadding*2)];
    [bPath addLineToPoint:CGPointMake(width, yPadding*2)];
    [bPath moveToPoint:CGPointMake(0, yPadding*3)];
    [bPath addLineToPoint:CGPointMake(width, yPadding*3)];
    //
    self.separaterLayer.path = bPath.CGPath;
}


#pragma mark - buttonDidClickedAction:
- (void)buttonDidClickedAction:(QMUIButton *)button {
    NSString *title = [button titleForState:UIControlStateNormal];
    if ([BConversionInputViewCharString rangeOfString:title].location==NSNotFound) {
        title = @"";
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(conversionInputViewDidInputString:)]) {
        [self.delegate conversionInputViewDidInputString:title];
    }
}


+ (CGSize)sizeFromWidth:(CGFloat)width needSafeBottomArea:(BOOL)needSafeBottomArea {
    //375*242
    CGFloat height = 242.0;
    if (width<=375.0) {
        height = width*(242.0/375.0);
    }
    height = needSafeBottomArea?(height+Height_Bottom_SafeArea):height;
    return CGSizeMake(width, height);
}

/**
 将当前输入字符串拼接到已经输入的当中
 */
+ (NSString *)string:(NSString *)string byAppendingString:(NSString *)appending digits:(NSInteger)digits {
    //
    if (!string) {
        string = @"";
    }
    if (appending.length) {
        if ([appending isEqualToString:@"<"]) {//delete
            NSInteger stringL = string.length;
            if (stringL>0) {
                string = [string substringToIndex:stringL-1];
            }
        }else {//add
            //判断是否有小数点
            NSRange pointRange = [string rangeOfString:@"."];
            BOOL hasPoint = pointRange.location!=NSNotFound;
            if (hasPoint) {
                //判断小数点位数
                NSString *subString = [string substringFromIndex:pointRange.location];
                NSInteger subLength = subString.length;
                if (subLength-1<digits) {
                    //如果不是点就拼接
                    if (![appending isEqualToString:@"."]) {
                        string = [string stringByAppendingString:appending];
                    }
                }
            }else {
                //直接拼接
                string = [string stringByAppendingString:appending];
            }
            //在此做优化处理
            /**
             如果第一个是小数点需要在前面+0
             */
            if ([string isEqualToString:@"."]) {
                string = [NSString stringWithFormat:@"0%@",string];
            }
            /**
             如果string全是0 就只显示一个0 如果有小数点就不用处理
             */
            if ([string rangeOfString:@"."].location==NSNotFound) {
                if (string.floatValue==0) {
                    return @"0";
                }else {
                    //设置一个上限 超过100000000
                    if (string.floatValue>100000000) {
                        string = [string substringToIndex:string.length-1];
                    }
                }
            }
        }
    }
    return string;
}

@end
